﻿using System.IO;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Win32;

namespace P6_note_wpf;

/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
    private string? currentFilePath;
    public MainWindow()
    {
        InitializeComponent();
    }

    private void File_Open(object sender, RoutedEventArgs e)
    {
        OpenFileDialog ofd = new OpenFileDialog { Filter = "Text Files (*.txt)|*.txt" };
        if (ofd.ShowDialog()==true)
        {
            currentFilePath = ofd.FileName;
            Box.Text=File.ReadAllText(ofd.FileName);
        }
        
    }

    private void File_Save(object sender, RoutedEventArgs e)
    {
        if (currentFilePath == null)
        {
            SaveFileDialog sfd = new SaveFileDialog
            {
                Filter = "文本文件 (*.txt)|*.txt"
            };
            if (sfd.ShowDialog() == true)
            {
                File.WriteAllText(sfd.FileName, Box.Text);
            }
        }
        else
        {
            File.WriteAllText(currentFilePath,Box.Text);
        }
    }

    private void File_Close(object sender, RoutedEventArgs e)
    {
        currentFilePath = null;
        Application.Current.Shutdown();
    }

    private void Edit_Back(object sender, RoutedEventArgs e)
    {
        if (Box.CanUndo==true)
        {
            Box.Undo();
        }
    }
    private void Edit_Copy(object sender, RoutedEventArgs e)
    {
        throw new NotImplementedException();
    }
    private void Edit_Paste(object sender, RoutedEventArgs e)
    {
        throw new NotImplementedException();
    }
    private void Edit_Clear(object sender, RoutedEventArgs e)
    {
        Box.Text = "";
    }
    private void Help_About(object sender, RoutedEventArgs e)
    {
        MessageBox.Show("thank you.");
    }
}